package com.alibaba.citrus.cr.price.repository.impl;


import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.price.repository.ActivityWriteRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crforecastsales.activityuserecordrelation.dto.CreateActivityUseRecordRelationResponse;
import com.epoch.app.crforecastsales.activityuserecordrelation.dto.DeleteActivityUseRecordRequest;
import com.epoch.app.crforecastsales.activityuserecordrelation.dto.DeleteActivityUseRecordResponse;
import com.epoch.app.crforecastsales.activityuserecordrelation.service.ActivityUseRecordRelationService;
import com.epoch.app.crforecastsales.domian.activityclientrelation.dto.CreateActivityClientRelationResponse;
import com.epoch.app.crforecastsales.domian.activityclientrelation.dto.QueryActivityClientRelationIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activityclientrelation.dto.UpdateActivityClientRelationForVersionResponse;
import com.epoch.app.crforecastsales.domian.activityclientrelation.service.ActivityClientRelationService;
import com.epoch.app.crforecastsales.domian.activityterm.dto.CreateActivityTermResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.QueryActivityTermForActivityIdRequest;
import com.epoch.app.crforecastsales.domian.activityterm.dto.QueryActivityTermForActivityIdResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.QueryActivityTermIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.UpdateActivityTermForVersionResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.UpdateOccupyDataForVersionResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.UpdateOccupyDataNotForVersionResponse;
import com.epoch.app.crforecastsales.domian.activityterm.dto.UpdateStatusForIdAndVersionRequest;
import com.epoch.app.crforecastsales.domian.activityterm.dto.UpdateStatusForIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activityterm.model.ActivityTerm;
import com.epoch.app.crforecastsales.domian.activityterm.service.ActivityTermService;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.dto.CreateActivityTermGiftRelationResponse;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.dto.QueryActivityTermGiftRelationIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.dto.UpdateActivityTermGiftRelationForVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytermgiftrelation.service.ActivityTermGiftRelationService;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.dto.CreateActivityTermItemRelationResponse;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.dto.QueryActivityTermItemRelationIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.dto.UpdateActivityTermItemRelationForVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytermitemrelation.service.ActivityTermItemRelationService;
import com.epoch.app.crforecastsales.domian.activitytitle.dto.*;
import com.epoch.app.crforecastsales.domian.activitytitle.service.ActivityTitleService;
import com.epoch.app.crforecastsales.domian.activitytitleext.dto.CreateActivityTitleExtResponse;
import com.epoch.app.crforecastsales.domian.activitytitleext.dto.QueryActivityTitleExtIdAndVersionRequest;
import com.epoch.app.crforecastsales.domian.activitytitleext.dto.QueryActivityTitleExtIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytitleext.dto.UpdateActivityTitleExtForVersionResponse;
import com.epoch.app.crforecastsales.domian.activitytitleext.service.ActivityTitleExtService;
import com.epoch.app.crforecastsales.domian.operatejournalflow.dto.CreateOperateJournalFlowResponse;
import com.epoch.app.crforecastsales.domian.operatejournalflow.dto.QueryOperateJournalFlowIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.operatejournalflow.dto.UpdateOperateJournalFlowForVersionResponse;
import com.epoch.app.crforecastsales.domian.operatejournalflow.service.OperateJournalFlowService;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.dto.CreateSegmentedActivityTermResponse;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.dto.QuerySegmentedActivityTermIdAndVersionResponse;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.dto.UpdateSegmentedActivityTermForVersionResponse;
import com.epoch.app.crforecastsales.domian.segmentedactivityterm.service.SegmentedActivityTermService;
import com.epoch.app.crforecastsales.model.dto.*;
import com.epoch.app.crpricecenter.sdo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/10/14
 * @Description 促销活动及坎级信息活动同步 repository
 */
@Repository
public class ActivityWriteRepositoryImpl implements ActivityWriteRepository {

    Log log = Log.getLogger(ActivityWriteRepositoryImpl.class);

    @Resource
    private ActivityTitleService activityTitleService;

    @Resource
    private ActivityTermService activityTermService;

    @Resource
    private ActivityClientRelationService activityClientRelationService;

    @Resource
    private ActivityTitleExtService activityTitleExtService;

    @Resource
    private ActivityTermItemRelationService activityTermItemRelationService;

    @Resource
    private ActivityTermGiftRelationService activityTermGiftRelationService;

    @Resource
    private SegmentedActivityTermService segmentedActivityTermService;

    @Resource
    private OperateJournalFlowService operateJournalFlowService;

    @Resource
    private ActivityUseRecordRelationService useRecordRelationService;


    @Override
    @RepositoryInvoker
    public Boolean createActivityTitleData(ActivityTitleSDO activityTitleSDO) {

        //查询数据是否存在
        QueryActivityTitleIdAndVersionRequest queryIdAndVersionRequest =new QueryActivityTitleIdAndVersionRequest();
        queryIdAndVersionRequest.setActivityId(activityTitleSDO.getActivityId());
        queryIdAndVersionRequest.setSender(activityTitleSDO.getSender());
        QueryActivityTitleIdAndVersionResponse queryIdAndVersionResponse = activityTitleService.queryActivityTitleIdAndVersion(queryIdAndVersionRequest);
        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            //无数据则执行创建操作

            ActivityTitleCreateRequest activityTitleCreateRequest = new ActivityTitleCreateRequest();

            BeanUtils.copyProperties(activityTitleSDO, activityTitleCreateRequest);
            activityTitleCreateRequest.setStatus(activityTitleSDO.getActivityStatus()
                    == null ? null : Integer.parseInt(activityTitleSDO.getActivityStatus()));

            //执行创建
            CreateActivityTitleResponse createResponse = activityTitleService.createActivityTitle(activityTitleCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }

        }else{
            ActivityTitleUpdateRequest activityTitleUpdateRequest = new ActivityTitleUpdateRequest();
            BeanUtils.copyProperties(activityTitleSDO, activityTitleUpdateRequest);
            activityTitleUpdateRequest.setId(queryIdAndVersionResponse.getId());
            activityTitleUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            activityTitleUpdateRequest.setStatus(activityTitleSDO.getActivityStatus()
                    == null ? null : Integer.parseInt(activityTitleSDO.getActivityStatus()));
            UpdateActivityTitleForVersionResponse updateActivityTitleForVersionResponse = activityTitleService.updateActivityTitleForVersion(activityTitleUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateActivityTitleForVersionResponse.getCount()) && updateActivityTitleForVersionResponse.getCount() > 0) {
                return true;
            }
        }



        return false;

    }

    @Override
    @RepositoryInvoker
    public Boolean createActivityTermData(ActivityTermSDO activityTermSDO) {
        String sender = "";
        if (StringUtils.isNotBlank(activityTermSDO.getFeatures())){
            sender = JSONObject.parseObject(activityTermSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        ActivityTermLoadListVO activityTermLoadListVO = new ActivityTermLoadListVO();
        activityTermLoadListVO.setActivityId(activityTermSDO.getActivityId());
        activityTermLoadListVO.setActivityTermId(activityTermSDO.getActivityTermId());
        activityTermLoadListVO.setSender(sender);
        QueryActivityTermIdAndVersionResponse queryIdAndVersionResponse = activityTermService.queryActivityTermIdAndVersion(activityTermLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            //无数据则执行创建操作

            ActivityTermCreateRequest activityTermCreateRequest = new ActivityTermCreateRequest();

            BeanUtils.copyProperties(activityTermSDO, activityTermCreateRequest);
            activityTermCreateRequest.setSender(sender);
            //执行创建
            CreateActivityTermResponse createResponse = activityTermService.createActivityTerm(activityTermCreateRequest);


            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }

        }else{

            ActivityTermUpdateRequest activityTermUpdateRequest = new ActivityTermUpdateRequest();

            BeanUtils.copyProperties(activityTermSDO, activityTermUpdateRequest);
            activityTermUpdateRequest.setId(queryIdAndVersionResponse.getId());
            activityTermUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            activityTermUpdateRequest.setSender(sender);
            UpdateActivityTermForVersionResponse updateActivityTermForVersionResponse = activityTermService.updateActivityTermForVersion(activityTermUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateActivityTermForVersionResponse.getCount()) && updateActivityTermForVersionResponse.getCount() > 0) {
                return true;
            }
        }

        return false;


    }

    @Override
    @RepositoryInvoker
    public Boolean createActivityClientRelationData(ActivityClientRelationSDO activityClientRelationSDO) {
        //本次提交007
        // 数据来源 sender
        String sender = "";
        if (StringUtils.isNotBlank(activityClientRelationSDO.getFeatures())){
            sender = JSONObject.parseObject(activityClientRelationSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        ActivityClientRelationLoadListVO activityClientRelationLoadListVO = new ActivityClientRelationLoadListVO();
        activityClientRelationLoadListVO.setActivityId(activityClientRelationSDO.getActivityId());
        activityClientRelationLoadListVO.setClientId(activityClientRelationSDO.getClientId());
        activityClientRelationLoadListVO.setSender(sender);
        QueryActivityClientRelationIdAndVersionResponse queryIdAndVersionResponse = activityClientRelationService.queryActivityClientRelationIdAndVersion(activityClientRelationLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            //无数据则执行创建操作

            ActivityClientRelationCreateRequest activityClientRelationCreateRequest = new ActivityClientRelationCreateRequest();

            BeanUtils.copyProperties(activityClientRelationSDO, activityClientRelationCreateRequest);
            activityClientRelationCreateRequest.setSender(sender);
            //执行创建
            CreateActivityClientRelationResponse createResponse = activityClientRelationService.createActivityClientRelation(activityClientRelationCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }

        }else{

            ActivityClientRelationUpdateRequest activityClientRelationUpdateRequest = new ActivityClientRelationUpdateRequest();

            BeanUtils.copyProperties(activityClientRelationSDO, activityClientRelationUpdateRequest);
            activityClientRelationUpdateRequest.setId(queryIdAndVersionResponse.getId());
            activityClientRelationUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            activityClientRelationUpdateRequest.setSender(sender);
            UpdateActivityClientRelationForVersionResponse updateActivityClientRelationForVersionResponse = activityClientRelationService.updateActivityClientRelationForVersion(activityClientRelationUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateActivityClientRelationForVersionResponse.getCount()) && updateActivityClientRelationForVersionResponse.getCount() > 0) {
                return true;
            }
        }

        return false;


    }

    @Override
    @RepositoryInvoker
    public Boolean createActivityTitleExtData(ActivityTitleExtSDO activityTitleExtSDO) {

        //查询数据是否存在
        QueryActivityTitleExtIdAndVersionRequest queryActivityTitleExtIdAndVersionRequest = new QueryActivityTitleExtIdAndVersionRequest();
        queryActivityTitleExtIdAndVersionRequest.setActivityNoteId(activityTitleExtSDO.getActivityNoteId());
        queryActivityTitleExtIdAndVersionRequest.setSender(activityTitleExtSDO.getSender());
        QueryActivityTitleExtIdAndVersionResponse queryIdAndVersionResponse = activityTitleExtService.queryActivityTitleExtIdAndVersion(queryActivityTitleExtIdAndVersionRequest);

        if (Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())) {

            //无数据则执行创建操作
            ActivityTitleExtCreateRequest activityTitleExtCreateRequest = new ActivityTitleExtCreateRequest();

            BeanUtils.copyProperties(activityTitleExtSDO, activityTitleExtCreateRequest);
            activityTitleExtCreateRequest.setActivityOccupyQuantity(0L);
            activityTitleExtCreateRequest.setActivityOccupyFee(0L);
            try {
                //执行创建
                CreateActivityTitleExtResponse createResponse = activityTitleExtService.createActivityTitleExt(activityTitleExtCreateRequest);
                if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                    return true;
                }
            } catch (DuplicateKeyException e) {
                log.error("ActivityWriteRepositoryImpl.createActivityTitleExtData_DuplicateKeyException_fail");
                log.error("重复的 key 异常", e);
                queryIdAndVersionResponse = activityTitleExtService.queryActivityTitleExtIdAndVersion(queryActivityTitleExtIdAndVersionRequest);
                return updateActivityTitleExt(activityTitleExtSDO, queryIdAndVersionResponse);
            }
        } else {
            return updateActivityTitleExt(activityTitleExtSDO, queryIdAndVersionResponse);
        }
        return false;

    }

    /**
     * 更新活动抬头扩展表
     * @param activityTitleExtSDO 扩展表
     * @param queryIdAndVersionResponse 查询结果
     * @return
     */
    private boolean updateActivityTitleExt(ActivityTitleExtSDO activityTitleExtSDO, QueryActivityTitleExtIdAndVersionResponse queryIdAndVersionResponse) {
        ActivityTitleExtUpdateRequest activityTitleExtUpdateRequest = new ActivityTitleExtUpdateRequest();

        BeanUtils.copyProperties(activityTitleExtSDO, activityTitleExtUpdateRequest);
        activityTitleExtUpdateRequest.setId(queryIdAndVersionResponse.getId());
        activityTitleExtUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
        activityTitleExtUpdateRequest.setActivityOccupyFee(queryIdAndVersionResponse.getActivityOccupyFee());
        activityTitleExtUpdateRequest.setActivityOccupyQuantity(queryIdAndVersionResponse.getActivityOccupyQuantity());

        UpdateActivityTitleExtForVersionResponse updateActivityTitleExtForVersionResponse = activityTitleExtService.updateActivityTitleExtForVersion(activityTitleExtUpdateRequest);

        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(updateActivityTitleExtForVersionResponse.getCount()) && updateActivityTitleExtForVersionResponse.getCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @RepositoryInvoker
    public Boolean createActivityTermItemRelationData(ActivityTermItemRelationSDO activityTermItemRelationSDO) {
        // 数据来源 sender
        String sender = "";
        if (StringUtils.isNotBlank(activityTermItemRelationSDO.getFeatures())){
            sender = JSONObject.parseObject(activityTermItemRelationSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        ActivityTermItemRelationLoadListVO activityTermItemRelationLoadListVO = new ActivityTermItemRelationLoadListVO();
        activityTermItemRelationLoadListVO.setActivityId(activityTermItemRelationSDO.getActivityId());
        activityTermItemRelationLoadListVO.setActivityTermId(activityTermItemRelationSDO.getActivityTermId());
        activityTermItemRelationLoadListVO.setItemId(activityTermItemRelationSDO.getItemId());
        activityTermItemRelationLoadListVO.setSender(sender);
        QueryActivityTermItemRelationIdAndVersionResponse queryIdAndVersionResponse = activityTermItemRelationService.queryActivityTermItemRelationIdAndVersion(activityTermItemRelationLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            //无数据则执行创建操作
            ActivityTermItemRelationCreateRequest activityTermItemRelationCreateRequest = new ActivityTermItemRelationCreateRequest();

            BeanUtils.copyProperties(activityTermItemRelationSDO, activityTermItemRelationCreateRequest);
            activityTermItemRelationCreateRequest.setSender(sender);
            //执行创建
            CreateActivityTermItemRelationResponse createResponse = activityTermItemRelationService.createActivityTermItemRelation(activityTermItemRelationCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }
        }else{

            ActivityTermItemRelationUpdateRequest activityTermItemRelationUpdateRequest = new ActivityTermItemRelationUpdateRequest();

            BeanUtils.copyProperties(activityTermItemRelationSDO, activityTermItemRelationUpdateRequest);
            activityTermItemRelationUpdateRequest.setId(queryIdAndVersionResponse.getId());
            activityTermItemRelationUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            activityTermItemRelationUpdateRequest.setSender(sender);
            UpdateActivityTermItemRelationForVersionResponse updateActivityTermItemRelationForVersionResponse = activityTermItemRelationService.updateActivityTermItemRelationForVersion(activityTermItemRelationUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateActivityTermItemRelationForVersionResponse.getCount()) && updateActivityTermItemRelationForVersionResponse.getCount() > 0) {
                return true;
            }
        }


        return false;


    }

    @Override
    @RepositoryInvoker
    public Boolean createActivityTermGiftRelationData(ActivityTermGiftRelationSDO activityTermGiftRelationSDO) {
        // 数据来源 sender
        String sender = "";
        if (StringUtils.isNotBlank(activityTermGiftRelationSDO.getFeatures())){
            sender = JSONObject.parseObject(activityTermGiftRelationSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        ActivityTermGiftRelationLoadListVO activityTermGiftRelationLoadListVO = new ActivityTermGiftRelationLoadListVO();
        activityTermGiftRelationLoadListVO.setActivityId(activityTermGiftRelationSDO.getActivityId());
        activityTermGiftRelationLoadListVO.setActivityTermId(activityTermGiftRelationSDO.getActivityTermId());
        activityTermGiftRelationLoadListVO.setGiftId(activityTermGiftRelationSDO.getGiftId());
        activityTermGiftRelationLoadListVO.setSender(sender);
        QueryActivityTermGiftRelationIdAndVersionResponse queryIdAndVersionResponse = activityTermGiftRelationService.queryActivityTermGiftRelationIdAndVersion(activityTermGiftRelationLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            //无数据则执行创建操作
            ActivityTermGiftRelationCreateRequest activityTermGiftRelationCreateRequest = new ActivityTermGiftRelationCreateRequest();

            BeanUtils.copyProperties(activityTermGiftRelationSDO, activityTermGiftRelationCreateRequest);
            activityTermGiftRelationCreateRequest.setSender(sender);
            //执行创建
                CreateActivityTermGiftRelationResponse createResponse = activityTermGiftRelationService.createActivityTermGiftRelation(activityTermGiftRelationCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }
        }else{

            ActivityTermGiftRelationUpdateRequest activityTermGiftRelationUpdateRequest = new ActivityTermGiftRelationUpdateRequest();

            BeanUtils.copyProperties(activityTermGiftRelationSDO, activityTermGiftRelationUpdateRequest);
            activityTermGiftRelationUpdateRequest.setId(queryIdAndVersionResponse.getId());
            activityTermGiftRelationUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            activityTermGiftRelationUpdateRequest.setSender(sender);
            UpdateActivityTermGiftRelationForVersionResponse updateActivityTermGiftRelationForVersionResponse = activityTermGiftRelationService.updateActivityTermGiftRelationForVersion(activityTermGiftRelationUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateActivityTermGiftRelationForVersionResponse.getCount()) && updateActivityTermGiftRelationForVersionResponse.getCount() > 0) {
                return true;
            }
        }

        return false;

    }

    @Override
    @RepositoryInvoker
    public Boolean createSegmentedActivityTermData(SegmentedActivityTermSDO segmentedActivityTermSDO) {
        // 数据来源 sender
        String sender = "";
        if (StringUtils.isNotBlank(segmentedActivityTermSDO.getFeatures())){
            sender = JSONObject.parseObject(segmentedActivityTermSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        SegmentedActivityTermLoadListVO segmentedActivityTermLoadListVO = new SegmentedActivityTermLoadListVO();
        segmentedActivityTermLoadListVO.setActivityId(segmentedActivityTermSDO.getActivityId());
        segmentedActivityTermLoadListVO.setActivityTermId(segmentedActivityTermSDO.getActivityTermId());
        segmentedActivityTermLoadListVO.setSegmentedActivityId(segmentedActivityTermSDO.getSegmentedActivityId());
        segmentedActivityTermLoadListVO.setSender(sender);
        QuerySegmentedActivityTermIdAndVersionResponse queryIdAndVersionResponse = segmentedActivityTermService.querySegmentedActivityTermIdAndVersion(segmentedActivityTermLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){

            SegmentedActivityTermCreateRequest segmentedActivityTermCreateRequest = new SegmentedActivityTermCreateRequest();

            BeanUtils.copyProperties(segmentedActivityTermSDO, segmentedActivityTermCreateRequest);
            segmentedActivityTermCreateRequest.setSender(sender);
            //执行创建
            CreateSegmentedActivityTermResponse createResponse = segmentedActivityTermService.createSegmentedActivityTerm(segmentedActivityTermCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }
        }else{

            SegmentedActivityTermUpdateRequest segmentedActivityTermUpdateRequest = new SegmentedActivityTermUpdateRequest();

            BeanUtils.copyProperties(segmentedActivityTermSDO, segmentedActivityTermUpdateRequest);
            segmentedActivityTermUpdateRequest.setId(queryIdAndVersionResponse.getId());
            segmentedActivityTermUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            segmentedActivityTermUpdateRequest.setSender(sender);
            UpdateSegmentedActivityTermForVersionResponse updateSegmentedActivityTermForVersionResponse = segmentedActivityTermService.updateSegmentedActivityTermForVersion(segmentedActivityTermUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateSegmentedActivityTermForVersionResponse.getCount()) && updateSegmentedActivityTermForVersionResponse.getCount() > 0) {
                return true;
            }
        }
        return false;

    }

    @Override
    @RepositoryInvoker
    public Boolean createOperateJournalFlowData(OperateJournalFlowSDO operateJournalFlowSDO) {
        // 数据来源 sender
        String sender = "";
        if (StringUtils.isNotBlank(operateJournalFlowSDO.getFeatures())){
            sender = JSONObject.parseObject(operateJournalFlowSDO.getFeatures()).getString("dataSource");
        }

        //查询数据是否存在
        OperateJournalFlowLoadListVO operateJournalFlowLoadListVO = new OperateJournalFlowLoadListVO();
        operateJournalFlowLoadListVO.setActivityId(operateJournalFlowSDO.getActivityId());
        operateJournalFlowLoadListVO.setActivityTermId(operateJournalFlowSDO.getActivityTermId());
        operateJournalFlowLoadListVO.setSegmentedActivityId(operateJournalFlowSDO.getSegmentedActivityId());
        operateJournalFlowLoadListVO.setOperateJournalFlowId(operateJournalFlowSDO.getOperateJournalFlowId());
        operateJournalFlowLoadListVO.setSender(sender);
        QueryOperateJournalFlowIdAndVersionResponse queryIdAndVersionResponse = operateJournalFlowService.queryOperateJournalFlowIdAndVersion(operateJournalFlowLoadListVO);

        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            OperateJournalFlowCreateRequest operateJournalFlowCreateRequest = new OperateJournalFlowCreateRequest();

            BeanUtils.copyProperties(operateJournalFlowSDO, operateJournalFlowCreateRequest);
            operateJournalFlowCreateRequest.setSender(sender);
            //执行创建
            CreateOperateJournalFlowResponse createResponse = operateJournalFlowService.createOperateJournalFlow(operateJournalFlowCreateRequest);

            if (StringUtils.isNotBlank(createResponse.getLastInsertId())) {
                return true;
            }

        }else{

            OperateJournalFlowUpdateRequest operateJournalFlowUpdateRequest = new OperateJournalFlowUpdateRequest();

            BeanUtils.copyProperties(operateJournalFlowSDO, operateJournalFlowUpdateRequest);
            operateJournalFlowUpdateRequest.setId(queryIdAndVersionResponse.getId());
            operateJournalFlowUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
            operateJournalFlowUpdateRequest.setSender(sender);
            UpdateOperateJournalFlowForVersionResponse updateOperateJournalFlowForVersionResponse = operateJournalFlowService.updateOperateJournalFlowForVersion(operateJournalFlowUpdateRequest);

            //存在数据则执行修改操作会返回修改的条数
            if (Objects.nonNull(updateOperateJournalFlowForVersionResponse.getCount()) && updateOperateJournalFlowForVersionResponse.getCount() > 0) {
                return true;
            }
        }

            return false;

    }

    @Override
    @RepositoryInvoker
    public Boolean updateActivityTitleData(ActivityTitleSDO activityTitleSDO) {

        //查询数据是否存在
        QueryActivityTitleIdAndVersionRequest queryActivityTitleIdAndVersionRequest = new QueryActivityTitleIdAndVersionRequest();
        queryActivityTitleIdAndVersionRequest.setActivityId(activityTitleSDO.getActivityId());
        queryActivityTitleIdAndVersionRequest.setSender(activityTitleSDO.getSender());
        QueryActivityTitleIdAndVersionResponse queryIdAndVersionResponse = activityTitleService.queryActivityTitleIdAndVersion(queryActivityTitleIdAndVersionRequest);

        //不存在数据
        if(Objects.isNull(queryIdAndVersionResponse) || Objects.isNull(queryIdAndVersionResponse.getId()) || Objects.isNull(queryIdAndVersionResponse.getVersion())){
            return false;
        }

        ActivityTitleUpdateRequest activityTitleUpdateRequest = new ActivityTitleUpdateRequest();

        BeanUtils.copyProperties(activityTitleSDO, activityTitleUpdateRequest);
        activityTitleUpdateRequest.setId(queryIdAndVersionResponse.getId());
        activityTitleUpdateRequest.setVersion(queryIdAndVersionResponse.getVersion());
        activityTitleUpdateRequest.setStatus(activityTitleSDO.getActivityStatus()
                == null ? null : Integer.parseInt(activityTitleSDO.getActivityStatus()));
        activityTitleUpdateRequest.setSender(activityTitleSDO.getSender());
        UpdateActivityTitleStatusAndDateForVersionResponse updateActivityTitleStatusAndDateForVersionResponse = activityTitleService.updateActivityTitleStatusAndDateForVersion(activityTitleUpdateRequest);

        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(updateActivityTitleStatusAndDateForVersionResponse.getCount()) && updateActivityTitleStatusAndDateForVersionResponse.getCount() > 0) {
            QueryActivityTermForActivityIdRequest termForActivityIdRequest = new QueryActivityTermForActivityIdRequest();
            termForActivityIdRequest.setActivityId(activityTitleSDO.getActivityId());
            termForActivityIdRequest.setSender(activityTitleSDO.getSender());
            QueryActivityTermForActivityIdResponse termForActivityIdResponse = activityTermService.queryActivityTermForActivityId(termForActivityIdRequest);
            if(Objects.nonNull(termForActivityIdResponse) || CollectionUtils.isNotEmpty(termForActivityIdResponse.getResult())){
                Boolean flag = true;
                List<ActivityTerm> activityTermList = termForActivityIdResponse.getResult();
                for (ActivityTerm activityTerm : activityTermList) {
                    UpdateStatusForIdAndVersionRequest updateStatusForIdAndVersionRequest = new UpdateStatusForIdAndVersionRequest();
                    updateStatusForIdAndVersionRequest.setId(activityTerm.getId());
                    updateStatusForIdAndVersionRequest.setStatus(activityTitleSDO.getActivityStatus()
                            == null ? null : Integer.parseInt(activityTitleSDO.getActivityStatus()));
                    updateStatusForIdAndVersionRequest.setVersion(activityTerm.getVersion());
                    updateStatusForIdAndVersionRequest.setSender(activityTitleSDO.getSender());
                    UpdateStatusForIdAndVersionResponse updateStatusForIdAndVersionResponse = activityTermService.updateStatusForIdAndVersion(updateStatusForIdAndVersionRequest);
                    if(Objects.isNull(updateStatusForIdAndVersionResponse) || updateStatusForIdAndVersionResponse.getCount() == 0) {
                        flag = false;
                    }
                }
                return flag;
            }

            return true;
        }

        return false;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean update(ActivityTermSDO sdo) {
        ActivityTermUpdateRequest activityTermUpdateRequest = new ActivityTermUpdateRequest();
        BeanUtils.copyProperties(sdo,activityTermUpdateRequest);
        UpdateOccupyDataForVersionResponse updateResponse = activityTermService.updateOccupyDataForVersion(activityTermUpdateRequest);
        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(updateResponse)&& Objects.nonNull(updateResponse.getCount()) && updateResponse.getCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean createActivityUseRecord(ActivityUseRecordSDO sdo) {
        ActivityUseRecordRelationCreateRequest createRequest = new ActivityUseRecordRelationCreateRequest();
        BeanUtils.copyProperties(sdo,createRequest);
        CreateActivityUseRecordRelationResponse response = useRecordRelationService.createActivityUseRecordRelation(createRequest);
        return Objects.nonNull(response) && StringUtils.isNotBlank(response.getLastInsertId());
    }

    @Override
    @RepositoryInvoker("逻辑删除活动使用记录信息")
    public Boolean deleteActivityUseRecord(ActivityUseRecordRelationSDO activityUseRecordRelationSDO) {
        DeleteActivityUseRecordRequest request = new DeleteActivityUseRecordRequest();
        request.setId(activityUseRecordRelationSDO.getId());
        request.setVersion(activityUseRecordRelationSDO.getVersion());
        DeleteActivityUseRecordResponse response = useRecordRelationService.deleteActivityUseRecord(request);

        if(Objects.nonNull(response) && Objects.nonNull(response.getCount()) && response.getCount() > 0){
            return true;
        }

        return false;
    }

    @Override
    @RepositoryInvoker("活动回补更新占用量和占用金额")
    public Boolean updateNotForVersion(ActivityTermSDO sdo) {
        ActivityTermUpdateRequest activityTermUpdateRequest = new ActivityTermUpdateRequest();
        BeanUtils.copyProperties(sdo,activityTermUpdateRequest);
        UpdateOccupyDataNotForVersionResponse updateResponse = activityTermService.updateOccupyDataNotForVersion(activityTermUpdateRequest);
        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(updateResponse)&& Objects.nonNull(updateResponse.getCount()) && updateResponse.getCount() > 0) {
            return true;
        }
        return false;
    }


    @Override
    @RepositoryInvoker("活动回补更新活动抬头扩展表占用量和占用金额")
    public Boolean updateNotForVersion(ActivityTitleExtSDO sdo) {
        ActivityTitleExtUpdateRequest activityTitleExtUpdateRequest = new ActivityTitleExtUpdateRequest();
        activityTitleExtUpdateRequest.setId(sdo.getId());
        activityTitleExtUpdateRequest.setActivityOccupyQuantity(sdo.getActivityOccupyQuantity());
        activityTitleExtUpdateRequest.setActivityOccupyFee(sdo.getActivityOccupyFee());

        com.epoch.app.crforecastsales.domian.activitytitleext.dto.UpdateOccupyDataNotForVersionResponse response
                = activityTitleExtService.updateOccupyDataNotForVersion(activityTitleExtUpdateRequest);
        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(response) && Objects.nonNull(response.getCount()) && response.getCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @RepositoryInvoker("活动回补更新活动抬头扩展表占用量和占用金额")
    public Boolean updateForVersion(ActivityTitleExtSDO sdo) {
        ActivityTitleExtUpdateRequest activityTitleExtUpdateRequest = new ActivityTitleExtUpdateRequest();
        activityTitleExtUpdateRequest.setId(sdo.getId());
        activityTitleExtUpdateRequest.setActivityOccupyQuantity(sdo.getActivityOccupyQuantity());
        activityTitleExtUpdateRequest.setActivityOccupyFee(sdo.getActivityOccupyFee());
        activityTitleExtUpdateRequest.setVersion(sdo.getVersion());

        com.epoch.app.crforecastsales.domian.activitytitleext.dto.UpdateOccupyDataForVersionResponse response
                = activityTitleExtService.updateOccupyDataForVersion(activityTitleExtUpdateRequest);
        //存在数据则执行修改操作会返回修改的条数
        if (Objects.nonNull(response) && Objects.nonNull(response.getCount()) && response.getCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateActivityTitleExtOccupyQuantityAndFee(ActivityTitleExtSDO sdo) {
        ActivityTitleExtUpdateRequest request = new ActivityTitleExtUpdateRequest();
        request.setId(sdo.getId());
        request.setActivityOccupyQuantity(sdo.getActivityOccupyQuantity());
        request.setActivityOccupyFee(sdo.getActivityOccupyFee());
        request.setVersion(sdo.getVersion());
        com.epoch.app.crforecastsales.domian.activitytitleext.dto.UpdateOccupyDataForVersionResponse response = activityTitleExtService.updateOccupyDataForVersion(request);
        if (Objects.nonNull(response) && Objects.nonNull(response.getCount()) && response.getCount() > 0) {
            return true;
        }
        return false;
    }
}
